package org.dsc.amafinder.control;

import java.util.HashSet;
import java.util.Set;

import org.dsc.amafinder.model.Favorito;
import org.dsc.amafinder.model.Pais;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Traductor de articulo a buscar por inode en amazon
 * @author DSC
 */
public class AmfFav {
   private static SharedPreferences shPrefs = null;
   
   public void initialize(final Activity ca) {
      shPrefs = ca.getPreferences(Context.MODE_PRIVATE);
      shPrefs.getAll();
   }
   
   public void add(final String asin, final double priceMin, final Pais paisMin) {
      SharedPreferences.Editor editor = shPrefs.edit();
      editor.putString(asin, asin + ";" + priceMin + ";" + paisMin);
      editor.commit();
   }

   public void del(final String asin) {
      SharedPreferences.Editor editor = shPrefs.edit();
      editor.remove(asin);
      editor.commit();
   }
   
   public Set<Favorito> getAll() {
      Set<Favorito> favoritos = new HashSet<Favorito>();
      Object[] objs = shPrefs.getAll().values().toArray();
      for (int i = 0; i < objs.length; i++) {
         String obj = (String) objs[i];
         favoritos.add(Favorito.read(obj));
      }
      return favoritos;
   }
}
